package logic

import (
	"context"
	"meituan-waimai/meituanrpc/meituanrpc"

	"meituan-waimai/api/internal/svc"
	"meituan-waimai/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CouponGrantLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCouponGrantLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CouponGrantLogic {
	return &CouponGrantLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CouponGrantLogic) CouponGrant(req *types.CouponGrantRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	grant, err := l.svcCtx.MeiTuanRpc.CouponGrant(l.ctx, &meituanrpc.CouponGrantRequest{
		CouponName:      req.CouponName,
		CouponThreshold: float32(req.CouponThreshold),
		CouponAmount:    float32(req.CouponAmount),
		CouponDiscount:  req.CouponDiscount,
		CouponType:      req.CouponType,
	})
	if err != nil {
		return &types.Response{
			Code: -1,
			Msg:  "发放优惠券失败",
			Data: err.Error(),
		}, nil
	}
	return &types.Response{
		Code: 200,
		Msg:  "发放优惠券成功",
		Data: grant,
	}, nil
}
